//
//  SpecialSubjectViewController.m
//  味·道
//
//  Created by lanou3g on 15/6/30.
//  Copyright (c) 2015年 WMR. All rights reserved.
//

#import "SpecialSubjectViewController.h"
#import "SpecialTableViewCell.h"
#import <UIView+WebCacheOperation.h>
#import "MJRefresh.h"
//#import <MJRefresh.h>
#import "SpecialDetailViewController.h"
#import "MBProgressHUD.h"
#import "weidaoAPI.h"
#import "MaocHeader.h"

@interface SpecialSubjectViewController ()
{
    MJRefreshHeader *_header;
    MJRefreshFooter *_footer;
}
@property (nonatomic,retain)UITableView *tableview;
@property (nonatomic,retain)NSMutableArray *dataArray;
@property (nonatomic,retain)MBProgressHUD *hud;
@end

@implementation SpecialSubjectViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    self.dataArray = [[NSMutableArray alloc]initWithCapacity:10];
    [self createCustomLab];
    [self createTableView];
    [self WL_createHUD];
    [self requestData];
}

- (void)createTableView{
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 55, SCREEN_WIDTH, SCREEN_HEIGHT-55) style:(UITableViewStylePlain)];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [_tableview registerClass:[SpecialTableViewCell class] forCellReuseIdentifier:@"special"];
    [self.view addSubview:_tableview];
}
- (void)createCustomLab{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(145, 18, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"精彩专题"];
    customLab.font = [UIFont boldSystemFontOfSize:18];
    [self.view addSubview:customLab];
    [customLab release];
}

- (void)requestData{
    RequestManager *manager = [[RequestManager alloc] initWithRequestURL:@"http://ibaby.ipadown.com/api/food/food.topic.list.php?p=1&pagesize=20&order=addtime" requestModel:GET requestParameter:nil];
    manager.delegate =self;
    [manager startRequest];
    [manager release];
}
- (void)WL_createHUD{
    _hud = [[[MBProgressHUD alloc] initWithView:self.tableview] autorelease];
    _hud.frame = self.view.bounds;
    _hud.minSize = CGSizeMake(100, 100);
    _hud.mode = MBProgressHUDModeIndeterminate;
    _hud.minShowTime = 2;
    _hud.cornerRadius = 5;
    [self.tableview addSubview:_hud];
    [_hud show:YES];
}
- (void)showProgressHUD{
    [self.view bringSubviewToFront:_hud];
    [_hud show:YES];
}
- (void)hideProgressHUD{
    _hud.labelText = @"加载完毕";
    [_hud hide:YES];
}
#warning delegate
//请求完毕后让代理对象执行的方法，同时把请求完整的data传给代理对象
- (void)requestDidFinishWithData:(NSData *)data{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
    NSArray *list = dic[@"results"];
    for (NSDictionary *diction in list) {
        SpecialModel *model = [[SpecialModel alloc] init];
        [model setValuesForKeysWithDictionary:diction];
        [self.dataArray addObject:model];
        [model release];
    }
    [self.tableview reloadData];
    [self hideProgressHUD];
}
//请求失败后把错误原因传值给代理对象
- (void)requestError:(NSError *)error{
    NSLog(@"error:%@",error);
}
#warning tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"special";
    SpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[SpecialTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:str];
    }
    SpecialModel *model = _dataArray[indexPath.row];
    [cell setModel:model];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SpecialDetailViewController *detailVC = [[SpecialDetailViewController alloc] init];
    SpecialModel *model = _dataArray[indexPath.row];
    detailVC.model = model;
    detailVC.Id = model.ID;
    [self.rightVC.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
